//
// Created by LONG on 2020/11/12.
//

#include "l1IntfUl.h"
#include "../l1ApiAdpt/l1ApiAdpt.h"

void UE_L2_L1I_UL_PtoProcess()
{
    uint32_t size;
    S_UeCommMsgHdr *pCommMsgHdr;

    while (1)
    {
        pCommMsgHdr = VarRB_ReadRequest(gpUeL2Context->pL3iL1iMsgQue, &size);
        IF_BREAK(==, NULL, pCommMsgHdr, NO_LOG_MESSAGE);

        switch (pCommMsgHdr->type)
        {
        case UE_L1_API_MSG_CONFIG_REQ:
            CSP_LOG_INF(UE_MD_L2_L1I, "UE_L2_L1I_UL_PtoProcess() recv UE_L1_API_MSG_CONFIG_REQ\n");
            gpUeL1ApiAdpt->ConfigReq(pCommMsgHdr);
            break;
        case UE_L1_API_MSG_SYNC_REQ:
            CSP_LOG_INF(UE_MD_L2_L1I, "UE_L2_L1I_UL_PtoProcess() recv UE_L1_API_MSG_SYNC_REQ\n");
            gpUeL1ApiAdpt->SyncReq(pCommMsgHdr);
            break;
        default:
            CSP_LOG_WRN(UE_MD_L2_L1I, "UE_L2_L1I_UL_PtoProcess() recv unsupport msg[%#x]\n", pCommMsgHdr->type);
            break;
        }
        VarRB_ReadConsume(gpUeL2Context->pL3iL1iMsgQue, pCommMsgHdr);
    }
}

void UE_L2_L1I_UL_TtiProcess(int32_t event, int fd, void *data)
{
}

void *UE_L2_L1I_UL_Task(void* arg)
{
    int fd;
    int32_t rc;

    fd = gpUeL2L1IntfContext->ulSelector.recvFd;
    POLLER_Add(gpUeL2L1IntfContext->ulPoller, fd, UE_L2_L1I_UL_TtiProcess, NULL, NULL, NULL);

    while (1)
    {
        rc = POLLER_Poll(gpUeL2L1IntfContext->ulPoller, 10);
        if (0 == rc)
        {
            /*
             * POLL TIME OUT处理，是为了在L1 START前处理能够处理L3等消息。
             * L1 START后就会有TTI消息上来，就不会超时进入此分支。
             */
            UE_L2_L1I_UL_PtoProcess();
        }
        pthread_testcancel();
    }
    return NULL;
}

